import { createRouter, createWebHistory } from 'vue-router'
import store from '../store'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta : {
      auth:true
    },
    children :[
      {
        path :"",
        name : "Main",
        component :() => import( '@/views/Main.vue'),
      },
      {
        path :"/user",
        name : "User",
        component :() => import( '@/views/User.vue'),
      },
      {
        path :"/request",
        name : "Request",
        component :() => import( '@/views/Request.vue'),
      },
      {
        path :"/request",
        name : "Request",
        component :() => import( '@/views/Request.vue'),
      },
      {
        path :"/game",
        name : "Game",
        component :() => import( '@/views/Game.vue'),
      }
    ],
  },
  
    {
      path: '/login',
      name: 'Login',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
      meta : {
        auth:false
      }
  }
]
let baseurl=""
if(process.env.NODE_ENV=="production"){
  baseurl="guanpaiadmins"
}
const router = createRouter({
  history: createWebHistory(baseurl),
  routes
})

router.beforeEach((to, from, next) => {
  /* 必须调用 `next` */
  if( to.meta.auth ){
    if(! store.state.userinfo.token){
      next({
        path: '/login'
      })
    }
    next()
  }
  
  next()
})
router.afterEach((to, from) => {
  const toDepth = to.path.split('/').length
  const fromDepth = from.path.split('/').length
  to.meta.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
})


export default router
